package com.issac.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.issac.common.R;
import com.issac.dto.SearchFormDto;
import com.issac.entity.Accessory;
import com.issac.entity.Fruit;
import com.issac.mapper.AccessoryMapper;
import com.issac.mapper.ContractMapper;
import com.issac.mapper.FruitMapper;
import com.issac.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName GoodsServiceImpl
 * @Author zy
 * @Date 2021/11/1 16:17
 * @Description GoodsServiceImpl
 * @Version 1.0
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    ContractMapper contractMapper;

    @Autowired
    FruitMapper fruitMapper;

    @Autowired
    AccessoryMapper accessoryMapper;

    @Override
    public R addFruit(Fruit fruit) {
        int result = 0;
        try {
            result = fruitMapper.insert(fruit);
            return R.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(result);
        }
    }

    @Override
    public R deleteGoodsById(String fruitId) {
        int result = 0;
        try {
            result = fruitMapper.deleteById(fruitId);

            // 删除对应的附属品
            QueryWrapper<Accessory> wrapper = new QueryWrapper<>();
            wrapper.eq("fruit_id",fruitId);
            accessoryMapper.delete(wrapper);

            // 删除对应的购销合同中间表
            contractMapper.deleteMidTabByFId(fruitId);

            return R.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(result);
        }
    }

    @Override
    public R updateFruit(Fruit fruit) {
        int result = 0;
        try {
            result = fruitMapper.updateById(fruit);
            return R.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(result);
        }
    }

    @Override
    public R selectAllGoods() {
        List<Fruit> list = null;
        try {
            list = fruitMapper.selectList(null);
            return R.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(list);
        }
    }

    @Override
    public R selectPageGoods(int page, int size) {
        List<Fruit> list = null;
        try {
            Page<Fruit> fruitPage = new Page<>(page, size);
            Page<Fruit> selectPage = fruitMapper.selectPage(fruitPage, null);
            list = selectPage.getRecords();

            long total = selectPage.getTotal();
            HashMap<String, Object> map = new HashMap<>();
            map.put("list",list);
            map.put("total",total);

            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(list);
        }
    }

    // 按条件查询商品（水果）
    @Override
    public R selectGoodsLike(SearchFormDto dto) {

        List<Fruit> list = null;
        try {
            //
            dto.setsP((dto.getsP()-1)*dto.getsPSize());
            list = fruitMapper.selectGoodsLike(dto);
            return R.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(list);
        }
    }
}
